package com.cloud.school.app.service.impl;

import com.cloud.school.app.entity.TbMajor;
import com.cloud.school.app.mapstruct.IMajorConvertMapper;
import com.cloud.school.app.model.ImageModel;
import com.cloud.school.app.model.MajorModel;
import com.cloud.school.app.repository.TbMajorMapper;
import com.cloud.school.app.service.ImageService;
import com.cloud.school.app.service.MajorService;
import com.cloud.school.app.service.UserService;
import com.cloud.school.app.util.IdWorker;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName MajorServiceImpl
 * @Description TODO
 * @Author 36297
 * @Date 2018/12/14 0:55
 * @Version 1.0
 */
@Service
public class MajorServiceImpl implements MajorService {

    @Autowired
    private TbMajorMapper tbMajorMapper;
    @Autowired
    private IMajorConvertMapper majorConvertMapper;
    @Autowired
    private IdWorker majorIdWorker;
    @Autowired
    private ImageService imageService;
    @Autowired
    private UserService userService;

    @Override
    @Transactional
    public boolean save(MajorModel majorModel) {
        TbMajor major = majorConvertMapper.modelToEntity(majorModel);
        major.setId(majorIdWorker.nextId());
        major.setCreateTime(System.currentTimeMillis());
        return tbMajorMapper.insertSelective(major) > 0;
    }

    @Override
    public List<MajorModel> selectByCollegeId(Long collegeId) {
        TbMajor major = new TbMajor();
        major.setCollegeId(collegeId);
        return majorConvertMapper.entityToModel(tbMajorMapper.select(major));
    }

    @Override
    public List<MajorModel> selectDetailByCollegeId(Long collegeId) {
        List<MajorModel> majorModels = selectByCollegeId(collegeId);
        appendImage(majorModels);
        Map<Long, Long> registerNum = userService.queryCollegeMajarRegisterNum(collegeId);
        majorModels.forEach(m -> {
            Long num = registerNum.get(m.getId());
            if (num != null) {
                m.setRegisterNum(num);
            }
        });
        return majorModels;
    }

    private void appendImage(List<MajorModel> majorModels) {
        List<Long> imageIds = majorModels.stream().map(m -> m.getImage()).filter(id -> id != null).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(imageIds)) {
            Map<Long, ImageModel> imageModelMap = imageService.seleteByIds(imageIds).stream().collect(Collectors.toMap(ImageModel::getId, i -> i));
            majorModels.forEach(m -> m.setImageModel(imageModelMap.get(m.getImage())));
        }
    }

    @Override
    public List<MajorModel> queryByIds(List<Long> majorIds) {
        if (CollectionUtils.isEmpty(majorIds)) {
            return Lists.newArrayList();
        }
        String majorIdArrayString = String.join(",", majorIds.stream().distinct().map(id -> id.toString()).collect(Collectors.toList()));
        return majorConvertMapper.entityToModel(tbMajorMapper.selectByIds(majorIdArrayString));
    }

    @Override
    @Transactional
    public boolean delete(Long majorId) {
        return tbMajorMapper.deleteByPrimaryKey(majorId) > 0;
    }

    @Override
    @Transactional
    public boolean deleteByCollegeId(Long collegeId) {
        TbMajor major = new TbMajor();
        major.setCollegeId(collegeId);
        return tbMajorMapper.delete(major) > 0;
    }
}
